/**
 * Copyright (C) 2010 - 2013 Harry Glasgow
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.jaden.ui.dialog.projectdefinition;

import com.googlecode.jaden.common.other.Utils;
import com.googlecode.jaden.ui.UiUtils;
import static com.googlecode.jaden.ui.Constants.*;
import com.googlecode.jaden.ui.dialog.BaseDialog;

import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.prefs.Preferences;

public class AddEditMapIOEntryDialog extends BaseDialog {

    private JTextField keyField;
    private JTextField valueField;
    private EditMapIODialog dialog;

    public AddEditMapIOEntryDialog(EditMapIODialog dialog, String key, double value, Preferences preferences) {
        super(dialog, Utils.isBlankOrNull(key) ? DIALOG_ADD_ENTRY_TITLE : DIALOG_EDIT_ENTRY_TITLE);
        this.dialog = dialog;

        JLabel keyLabel = new JLabel(Utils.translate(LABEL_KEY_TEXT));
        keyField = new JTextField(20);
        keyField.setEnabled(Utils.isBlankOrNull(key));
        keyField.addKeyListener(new KeyListener() {
            public void keyTyped(KeyEvent e) {
            }

            public void keyPressed(KeyEvent e) {
            }

            public void keyReleased(KeyEvent e) {
                enableButtons();
            }
        });
        JLabel valueLabel = new JLabel(Utils.translate(LABEL_VALUE_TEXT));
        valueField = new JTextField(20);
        valueField.addKeyListener(new KeyListener() {
            public void keyTyped(KeyEvent e) {
            }

            public void keyPressed(KeyEvent e) {
            }

            public void keyReleased(KeyEvent e) {
                enableButtons();
            }
        });
        UiUtils.enWidth(keyLabel, valueLabel);
        JPanel mainPanel = UiUtils.verticalList(UiUtils.horizontalList(keyLabel, keyField),
                UiUtils.horizontalList(valueLabel, valueField));
        setMainPanel(mainPanel, preferences);

        keyField.setText(key);
        valueField.setText(String.valueOf(value));
        enableButtons();
    }

    private void enableButtons() {
        boolean k = false;
        try {
            String s = valueField.getText();
            if (!Utils.isBlankOrNull(s)) {
                Double.parseDouble(s);
                if (!Utils.isBlankOrNull(keyField.getText())) {
                    k = true;
                }
            }
        } catch (NumberFormatException e) {
        }
        okButton.setEnabled(k);
    }

    protected void doOk() {
        dialog.putKV(keyField.getText(), Double.parseDouble(valueField.getText()));
        dispose();
    }
}